<?php
namespace app\admin\controller\search;

use app\common\model\InStore;
use app\common\model\OutStore;
use app\common\model\Store;
use app\common\model\Goods;

use app\admin\classes\BasicSearch;
use core\Api;
use core\V;
use core\Res;

/**
 * 产品
 */


class ByGoods extends \C {


  static $title=[
    "show"=>true,
    "title"=>"产品库存查询",
    "breadcrumb"=>["查询","库存查询","产品库存查询"],
    "subtitle"=>"查询产品库存信息",
  ];



  public function index(){

    $conf=[];
    $conf["title"]=static::$title;
    $this->wapper([
      "show"=>$conf["title"]["show"],
      "title"=>$conf["title"]["title"],
      "breadcrumb"=>$conf["title"]["breadcrumb"],
      "panel"=>isset($conf["title"]["panel"]),
      "subtitle"=>$conf["title"]["subtitle"],

    ]);
    $this->VD([
      "apiBase"=>static::$hash,
    ]);

    return view("search/bygoods",$this->VD());
  }
  public function getresult(){
    $code=input("form.code");
    empty($code) and Api::error("请输入产品编码");

    $goods_id=Goods::where("code",$code)->value("id");
    empty($goods_id) and Api::error("未找到产品");


    $list=[];

    $res=db()->query("
    select
      `a`.`goods_id` as `goods_id`,
      `b`.`code` as `goods_code`,
      `b`.`name` as `goods_name`,
      `a`.`store_id` as `store_id`,
      `c`.`name` as `store_name`,
      SUM(a.num) as `num`
    from (
      (select goods_id,store_id,SUM(num) as num
      from `in_store` where `status`=2
        and goods_id=:goods_id
      group by `store_id`,`goods_id`)

      union all

      (select goods_id,store_id,-SUM(num) as num
      from `out_store` where `status`=2
        and goods_id=:goods_id2
      group by `store_id`,`goods_id`)

      ) as `a`
      left join `goods` as `b` on `a`.`goods_id`=`b`.`id`
      left join `store` as `c` on `a`.`store_id`=`c`.`id`
    group by `a`.`store_id`,`a`.`goods_id`
    ",[
      "goods_id"=>$goods_id,
      "goods_id2"=>$goods_id,
    ]);

    $list=$res;


    Api::ok($list);

  }




 }
